How to use DataTable.Select() when the DataColumn.ColumnName contains a space?

I have a DataColumn that contains an space and I just couldn’t get the DataTable.Select(“Column Name=value) function to work.

So i found a solution on some other guys wordpress blog here.

The answer should be obvious to those who use SQL. In SQL to use a space, it often adds square brackets around the column names. [Column Name]. Yes, using square brackets is the solution.

String colName = "Column Name";
String Value = "some data";
DataTable.Select("[" + colName + "]='" + value + "'");

6 Comments

  1. yan franco says:

    Thank a lot brother!

  2. Deepak kumar Gupta says:

    Thanks a lot, after a lot of research on Google, finally i got the output as required with your help.

  3. ramz says:

    can anyone help me how to fix this... i should get records because id is greater than 1

    Dim d As New DataSet2

    Dim table As DataTable = d.Tables("ClientsPersonal")

    ' Presuming the DataTable has a column named Date.
    Dim expression As String
    expression = "id > 1"
    Dim foundRows() As DataRow

    ' Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression)
    MsgBox(foundRows.Count)
    Dim i As Integer
    ' Print column 0 of each returned row.
    For i = 0 To foundRows.GetUpperBound(0)
    MsgBox(foundRows(i)(0))
    Next i

    • rhyous says:

      Where are you populating the table from? Are you populating from a database or elsewhere? Can you verify that the table is populated?

      I see that you run Table.Select(), but that doesn't populate the table, it queries an already populated table.

      Assuming your table is populated, I cannot see a problem with the syntax, though I am a C# more than vb guy.

      http://rhyous.com/2010/05/28/how-to-query-a-database-in-c/

    • rhyous says:

      Also, this post may help you.
      http://rhyous.com/2010/02/17/how-read-use-an-ado-net-dataset-to-read-xml-files-designed-with-nested-attributes/

  4. Gulab says:

    Thanks very much. Helped me.

Leave a Reply

How to post code in comments?